{% extends 'admin/change_list.html' %}

{% load i18n admin_static %}

{% block object-tools-items %}
    {{ block.super }}
    <li>
        {% if has_add_permission %}
            <!-- 这里是增加的按钮，要增加按钮在这里添加 -->
            <a href="{% url 'blog:add_info' %}?page={{ request.path }}&phones="
               class="addlink related-widget-wrapper-link"
               id="add-info"
               style="background-color:green">发送 信息</a>
        {% endif %}
    </li>

    <script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
    <script type="text/javascript">
        let check_all_set = new Set();
        let add_info_url = $("#add-info").attr("href");
        let table_thead_choice_name = "ID"; // 设置要选取的 thead 名称
        let table_thead_choice = 1; // thead 默认顺序值
        console.log(add_info_url);
        $(document).ready(function () {
            let table_thead = $("#result_list thead");
            {#console.log(table_thead);#}
            if (table_thead.length > 0) {
                let table_thead_text = table_thead[0].innerText;
                let table_thead_list = table_thead_text.replace(/\s+/g, ",").replace(/^,+|,*$/g, "").split(",");
                table_thead_choice = table_thead_list.indexOf(table_thead_choice_name);
                {#console.log(table_thead_list);#}
                {#console.log(table_thead_choice);#}
            }

            $(".action-select").change(function () {
                // 多选时，eq(2) 取列数据, 2 是第几列
                if (table_thead_choice != 0) {
                    phone = $(this).closest('tr').find('td').eq(table_thead_choice).text();
                } else {
                    phone = $(this).closest('tr').find('th').eq(table_thead_choice).text();
                }
                if ($(this).prop("checked") === true) {
                    {#console.log(phone);#}
                    check_all_set.add(phone);
                } else {
                    check_all_set.delete(phone);
                }
                let check_value = $(this).attr("value");
                {#console.log(check_value);#}
                {#$(this).closest('tr').find('td').eq(4).html("修改成功");#}
                {#console.log(phone);#}
                {#console.log(check_all_set);#}
                let check_all_str = Array.from(check_all_set).join(",");
                $("#add-info").attr("href", add_info_url + check_all_str);
            });

            $("#action-toggle").change(function () {
                if ($(this).prop("checked") === true) {
                    $(".results table").each(function () {
                        {#var sub_value = $(this).find("tr").eq(2).find("td").eq(0).html();#}
                        let par_value = $(this).find("tr");
                        {#alert(sub_value);#}
                        for (let i = 1; i < par_value.length; i++) {
                            // 全选时，eq(2) 取列数据, 2 是第几列
                            if (table_thead_choice != 0) {
                                phone = par_value.eq(i).find("td").eq(table_thead_choice).text();
                            } else {
                                phone = par_value.eq(i).find("th").eq(table_thead_choice).text();
                            }

                            {#console.log(phone);#}
                            check_all_set.add(phone);
                        }
                    });
                    {#console.log("choice-all");#}
                } else {
                    check_all_set.clear();
                    {#console.log("cancel-all")#}
                }
                {#console.log(check_all_set);#}
                let check_all_str = Array.from(check_all_set).join(",");
                $("#add-info").attr("href", add_info_url + check_all_str);
            });
        });

    </script>
{% endblock %}

